package com.kuang.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author luomingliang
 * @date 2021/5/14 11:58
 */
public class CallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        new Thread(new Runnable()).start();
//        new Thread(new FutureTask<V>()).start();
//        new Thread(new FutureTask<V>(Callable)).start();

        MyThread myThread = new MyThread();
        //适配类
        FutureTask<Integer> futureTask = new FutureTask<>(myThread);
        new Thread(futureTask, "A").start(); // 怎么启动Callable
        new Thread(futureTask, "B").start(); // 结果会被缓存
        //获取Callable的返回结果
        Integer o = futureTask.get(); //这个get 方法可能会产生阻塞 把他放到最后
        //或者使用异步通信来处理
        System.out.println(o);
    }
}

class MyThread implements Callable<Integer> {

    @Override
    public Integer call() {
        System.out.println("call()"); //两个线程会打印几个call
        //耗时的操作
        return 1024;
    }
}